#coding:utf8
from config import *
from config_mouse_start import *
from routin_base import *

def x2_jump():
    cfg.x2_mode = False
    set_cfg_sum_0()
    if cfg.enable_jump_task:
        cfg.jump_task = (cfg.x2_mode_dst[0],cfg.x2_mode_dst[1])
    else:
        con_set_pos(cfg.x2_mode_dst[0],cfg.x2_mode_dst[1])
        clear(True)

def x2_d_to_dst():
    dx = (cfg.snap[-1][1] -  cfg.snap[0][1]) * cfg.x2_mode_d / 100 + cfg.snap[0][1]
    dy = (cfg.snap[-1][2] -  cfg.snap[0][2]) * cfg.x2_mode_d / 100 + cfg.snap[0][2]
    cfg.x2_mode_dst = (round(dx), round(dy))

def deal_x2_mode_num():
    st,en = cfg.snap[0],cfg.snap[-1]
    st = int(st[0])
    en = int(en[0])
    if en-st<=0:
        return        
    f = float(cfg.sum)
    if f<st:
        return            
    cfg.x2_mode_d = (f-st)*100/(en-st)
    x2_d_to_dst()

def deal_x2_mode(a):
    if a>=0 and a<=10:
        cfg.sum = deal_sum(cfg.sum, a, sum_max_l)
        deal_x2_mode_num()
        return
    if a==16:
        set_cfg_sum_0()
        return
    if a==14:
        if len(cfg.sum)==1:
            set_cfg_sum_0()
        else:
            cfg.sum = cfg.sum[0:-1]
        return
    if a==15:        
        if cfg.sum=='0':
            cfg.x2_mode = False
            con_set_pos(cfg.x2_mode_start_pos[0],cfg.x2_mode_start_pos[1])
            return    
        x2_jump()
        return       
    if a==12:
        x2_jump()
        cfg.click_time = time.time()    

    if a==11:
        en = int(cfg.snap[-1][0])
        st = int(cfg.snap[0][0])
        if cfg.sum=='0':
            cfg.sum = str(st)
            deal_x2_mode_num()
            return
        
        cfg.sum = str(int(float(cfg.sum)+1))
        deal_x2_mode_num()
        return
    
    if a==13:
        en = cfg.snap[-1][0]
        st = int(cfg.snap[0][0])
        if cfg.sum=='0':
            cfg.sum = str(en)        
        if float(cfg.sum)<=st:
            cfg.sum = str(st)        
            deal_x2_mode_num()
            return    
        cfg.sum = str(math.ceil(float(cfg.sum)-1))
        deal_x2_mode_num()
            
def deal_key_x2_mode(st,a):
    if 'caps' in st:
        exit_x2_mode()
        return
    if a >=0:        
        deal_x2_mode(a)
        return
    
    if pause_str in st:
        from routin_scroll import deal_pause
        deal_pause()
        return
    
    elif scroll_str in st:
        x2_jump()
        return
    else:
        cfg.x2_mode = False    
        clear(True)           

def exit_x2_plain(exitdu=False):
    cfg.x2_mode=False
    set_cfg_sum_0()
    con_set_pos(cfg.x2_mode_start_pos[0],cfg.x2_mode_start_pos[1])
    if is_sca() and exitdu:
        du_exit()

def exit_x2_mode():
    from routin_simple import set_to_abs_len,update_du
    if not cfg.x2_mode:
        return
    cfg.x2_mode = False
    set_cfg_sum_0()    
    cfg.x2_mode_exit_time = time.time()
    con_set_pos(cfg.x2_mode_start_pos[0],cfg.x2_mode_start_pos[1])
    ll = cal_dis(cfg.x2_mode_dst,cfg.x2_mode_start_posp)
    set_to_abs_len(ll-cfg.x2_mode_dd)
    cfg.sca_noshow_last = not cfg.last_has_color
    if not is_sca():
        update_du(True)

def into_x2_mode():
    from routin_simple import get_snap_sort,getp,xing
    if cfg.x2_mode:
        return
    
    if not cfg.w2showing:
        return
    
    a = get_snap_sort()        
    if len(a) ==0:
        return                    
    if len(a) ==1:
        x,y = getp()
        cfg.snap = [('0',x,y),a[0]]    
    else:
        cfg.snap = a
    cfg.x2_mode = True
    cfg.x2_mode_slow = False
    
    dd = cal_dis( (cfg.snap[-1][1],cfg.snap[-1][2]),(cfg.snap[-2][1],cfg.snap[-2][2]))
    
    cfg.x2_mode_dd = dd
    cfg.x2_mode_start_pos = get_cur_pos()
    cfg.x2_mode_start_posp = getp()
    cfg.x2_mode_d = 100
    x2_d_to_dst()    
    cfg.sum = str(cfg.snap[-1][0])
    cfg.x2_ini_mouse=0
    
def deal_x2_move(x, y,isLeft=False):
    dx = cfg.snap[1][1] - cfg.snap[0][1] 
    dy = cfg.snap[1][2] - cfg.snap[0][2]
    du = math.atan2(dy, dx)
    du2 = math.atan2(y, x)
    d = du2 - du
    if d >  math.pi:
        d -= 2 * math.pi
    if d < - math.pi:
        d += 2 * math.pi
    dd = math.cos(d) *math.sqrt(x * x + y * y) *get_x2_d_sca(isLeft)
    cfg.x2_mode_d += dd    
    cfg.x2_mode_d = max(0, cfg.x2_mode_d)        
    x2_d_to_dst()            

def half_next():
    ll = []
    for i in cfg.draw_set:
        ll.append(int(float(i)))
    ll = sorted(ll)
    if len(ll)<3:
        return False
    dis = ll[-1]-ll[-2]
    cur = float(cfg.sum)
    if cur>ll[-1]:
        cfg.sum = str(ll[-1])
        deal_x2_mode_num()    
        return True
    ll.reverse()
    
    for i in ll:
        if i<cur and cur-i>dis:
            if i+dis in ll:
                return False
            cfg.sum = str(i)
            deal_x2_mode_num()    
            return True
    return False

def x2_half_move():
    if half_next():
        return
    
    if cfg.x2_mode_d == 0:
        cfg.x2_mode_d = 100
    else:
        cfg.x2_mode_d -= 33
        if cfg.x2_mode_d < 0:
            cfg.x2_mode_d = 0
    find_near_d()
    for i in range(1000):
        if cfg.sum in cfg.draw_set:
            break
        if str(int(cfg.sum)+i) in cfg.draw_set:
            cfg.sum = str(int(cfg.sum)+i)
            break
    deal_x2_mode_num()    